<template>
  <a-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
    <erp-festival-form ref="realForm" @ok="submitCallback" :disabled="disableSubmit" normal></erp-festival-form>
    <div class="drawer-footer">
      <a-button v-if="!disableSubmit"  @click="handleOk" type="primary" v-has="'festival:add'" style="margin-bottom: 0;">保存</a-button>
      <a-popconfirm v-if="flag" title="确定删除吗?" @confirm="() => deleteById()">
        <a-button type="danger"  v-has="'festival:delete'" style="margin-bottom: 0;background-color:#FF7875">删除</a-button>
      </a-popconfirm>
    </div>
  </a-drawer>
</template>

<script>

  import ErpFestivalForm from './ErpFestivalForm'
  import { deleteAction } from '@/api/manage'
  import moment from 'moment'

  export default {
    name: 'ErpFestivalModal',
    components: {
      ErpFestivalForm
    },
    data () {
      return {
        title:"操作",
        dateList: [],
        startTime: '',
        width:400,
        visible: false,
        disableSubmit: false,
        flag: false,
        recordDate: {},
        url: {
          deleteFestival: "/api/erp/hr/erpFestival/delete"
        }
      }
    },
    methods: {
      add () {
        this.visible=true
        this.flag = false
        this.$nextTick(()=>{
          this.recordDate = {}
          this.$refs.realForm.dateList = this.dateList
          this.$refs.realForm.add(moment(this.startTime, 'YYYY-MM-DD'));
        })
      },
      edit (record) {
        this.visible = true
        this.flag = true
        this.$nextTick(()=>{
          record.beginTime = moment(record.beginTime, 'YYYY-MM-DD')
          record.endTime = moment(record.endTime, 'YYYY-MM-DD')
          this.recordDate = record
          this.$refs.realForm.dateList = this.dateList
          this.$refs.realForm.edit(this.recordDate);
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      submitCallback(){
        this.$emit('ok');
        this.visible = false;
      },
      handleOk () {
        this.$refs.realForm.submitForm();
      },
      deleteById() {
        const that = this
        deleteAction(this.url.deleteFestival, { id: this.recordDate.id }).then((res)=>{
          if(res.success){
            that.$message.success(res.message);
            that.submitCallback();
          }else{
            that.$message.warning(res.message);
          }
        })
      },
      handleCancel () {
        // this.close()
      }
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
  .drawer-footer{
    position: absolute;
    bottom: -8px;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }
</style>
